home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / os2 / pmnos11s / pop2cli.c < prev    next >
C/C++ Source or Header  |  1993-07-30  |  4KB  |  166 lines

  1. /* POP2 Client routines -- Not recommended.
  2.  *    Jan 92    William Allen Simpson
  3.  *        complete re-write to match new mailreader commands
  4.  *
  5.  *    partly based on a NNTP client design by Anders Klemets, SM0RGV
  6.  *    Originally authored by Mike Stockett (WA7DYX).
  7.  *
  8.  * History:
  9.  *    Modified 12 May 1991 by Mark Edwards (WA6SMN) to use new timer
  10.  *    facilities in NOS0423.  Fixed type mismatches spotted by C++.
  11.  *    Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  12.  *      with later releases (NOS0522).
  13.  *    Added into NOS by PA0GRI (and linted into "standard" C)
  14.  *     Modified 14 June 1987 by P. Karn for symbolic target addresses,
  15.  *      also rebuilt locking mechanism
  16.  *
  17.  *    Some code culled from previous releases of SMTP.
  18.  *    See that code for applicable copyright notices.
  19.  */
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include "global.h"
  23. #include "timer.h"
  24. #include "proc.h"
  25. #include "netuser.h"
  26. #include "socket.h"
  27. #include "cmdparse.h"
  28. #include "files.h"
  29. #include "mailcli.h"
  30. #include "mailutil.h"
  31. #include "smtp.h"
  32. #if defined(PM)
  33. extern void MailNotify(char *pszUser);
  34. #endif
  35.  
  36. extern char Badhost[];
  37.  
  38. /* Response string keys */
  39. static char *greeting_rsp  = "+ POP2 ";
  40.  
  41. void
  42. pop2_job(unused,v1,p2)
  43. int unused;
  44. void *v1;
  45. void *p2;
  46. {
  47.     struct mailservers *np = v1;
  48.     struct sockaddr_in fsocket;
  49.     char buf[TLINELEN];
  50.     char *cp;
  51.     FILE *wfp = NULLFILE;
  52.     int s = -1;
  53.     int folder_len;
  54.     int msg_num = 0;
  55.  
  56.     if ( mailbusy( np ) )
  57.         return;
  58.  
  59.     if ( (fsocket.sin_addr.s_addr = resolve(np->hostname)) == 0L ) {
  60.         /* No IP address found */
  61.         if (Mailtrace >= 1)
  62.             log(-1,"POP2 can't resolve host '%s'", np->hostname);
  63.         start_timer(&np->timer);
  64.         return;
  65.     }
  66.  
  67.     fsocket.sin_family = AF_INET;
  68.     fsocket.sin_port = IPPORT_POP2;
  69.  
  70.     s = socket(AF_INET,SOCK_STREAM,0);
  71.     sockmode(s,SOCK_ASCII);
  72.  
  73.     if (connect(s,(char *)&fsocket,SOCKSIZE) == -1) {
  74.         cp = sockerr(s);
  75.         if (Mailtrace >= 2)
  76.             log(s,"POP2 Connect failed: %s",
  77.                 cp != NULLCHAR ? cp : "");
  78.         goto quit;
  79.     }
  80.  
  81.     log(s,"POP2 Connected to mailhost %s", np->hostname);
  82.  
  83.     if ( mailresponse( s, buf, "banner" ) == -1)
  84.         goto quit;
  85.  
  86.     if (strncmp(buf,greeting_rsp,strlen(greeting_rsp)) != 0)
  87.         goto quitcmd;
  88.  
  89.     (void)usprintf(s,"HELO %s %s\n",np->username,np->password);
  90.  
  91.     if ( mailresponse( s, buf, "HELO" ) == -1)
  92.         goto quit;
  93.  
  94.     if (buf[0] != '#'
  95.       || (folder_len = atoi(&(buf[1]))) == 0) {
  96.         /* If there is no mail (the only time we get a "+"
  97.          * response back at this stage of the game),
  98.          * then just close out the connection, because
  99.          * there is nothing more to do!! */
  100.         goto quitcmd;
  101.     }
  102.  
  103.     if ((wfp = tmpfile()) == NULLFILE) {
  104.         if ( Mailtrace >= 1 )
  105.             log(s,"POP2 Cannot create %s", "tmp file" );
  106.         goto quitcmd;
  107.     }
  108.  
  109.     (void)usprintf(s,"READ\n");
  110.  
  111.     /* now, get the text */
  112.     while(TRUE) {
  113.         long msg_len;
  114.  
  115.         if ( mailresponse( s, buf, "read loop" ) == -1)
  116.             goto quit;
  117.  
  118.         if (buf[0] == '='
  119.          && (msg_len = atol(&(buf[1]))) > 0) {
  120.             (void)usprintf(s,"RETR\n");
  121.  
  122.             while ( msg_len > 0 ) {
  123.                 if (recvline(s,buf,TLINELEN) == -1)
  124.                     goto quit;
  125.  
  126.                 rip(buf);
  127.                 fprintf(wfp,"%s\n",buf);
  128.  
  129.                 msg_len -= (long)(strlen(buf)+2);/* Add CRLF */
  130.             }
  131.             (void)usprintf(s,"ACKD\n");
  132.  
  133.             msg_num++;
  134.         } else {
  135.             break;
  136.         }
  137.     }
  138.  
  139.     if ( folder_len > 0 ) {
  140.         /* testing the result is pointless,
  141.          * since POP2 already deleted mail.
  142.          */
  143.         copymail( Mailspool, np->mailbox, buf, TLINELEN, wfp, Mailtrace );
  144.  
  145.         tprintf("New mail arrived for %s from mailhost %s%c\n",
  146.                 np->mailbox,
  147.                 np->hostname,
  148.                 Mailquiet ? ' ' : '\007');
  149. #if defined(PM)
  150.         MailNotify(np->mailbox);
  151. #endif
  152.     }
  153.  
  154. quitcmd:
  155.     (void)usprintf(s,"QUIT\n");
  156.  
  157. quit:
  158.     log(s,"POP2 daemon exiting");
  159.     (void) close_s(s);
  160.  
  161.     if (wfp != NULLFILE)
  162.         fclose(wfp);
  163.     start_timer(&np->timer);
  164. }
  165.  
  166.